package com.leetcode;


/**
 *
 */
public class 链表两数相加 {

    /**
     * 笨方法，直接死循环 我写的
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = new ListNode();
        ListNode index = root;
        boolean isPre = false;
        while (l1 != null && l2 != null) {
            int vvvv = l1.val + l2.val;
            if (isPre) {
                vvvv += 1;
            }
            ListNode newNode = null;
            if (vvvv <= 9) {
                index.val = vvvv;
                isPre = false;
            } else {
                index.val = vvvv % 10;
                isPre = true;
            }
            if (l1.next != null || l2.next != null) {
                newNode = new ListNode();
                index.next = newNode;
            } else if (l2.next == null && l1.next == null && isPre) {
                newNode = new ListNode(1);
                index.next = newNode;
                isPre = false;
            }
            l1 = l1.next;
            l2 = l2.next;
            index = newNode;
        }
        while (l1 != null && l2 != null) {
            int vvvv = l1.val + l2.val;
            if (isPre) {
                vvvv += 1;
            }
            ListNode newNode = null;
            if (vvvv <= 9) {
                index.val = vvvv;
                isPre = false;
            } else {
                index.val = vvvv % 10;
                isPre = true;
            }
            if (l1.next != null) {
                newNode = new ListNode();
                index.next = newNode;
                index = newNode;
            }
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            int vvvv = l1.val;
            if (isPre) {
                vvvv += 1;
            }
            ListNode newNode = null;
            if (vvvv <= 9) {
                index.val = vvvv;
                isPre = false;
            } else {
                index.val = vvvv % 10;
                isPre = true;
            }
            if (l1.next != null) {
                newNode = new ListNode();
                index.next = newNode;
            } else if (l1.next == null && isPre) {
                newNode = new ListNode(1);
                index.next = newNode;
                isPre = false;
            }
            l1 = l1.next;
            index = newNode;
        }
        while (l2 != null) {
            int vvvv = l2.val;
            if (isPre) {
                vvvv += 1;
            }
            ListNode newNode = null;
            if (vvvv <= 9) {
                index.val = vvvv;
                isPre = false;
            } else {
                index.val = vvvv % 10;
                isPre = true;
            }
            if (l2.next != null) {
                newNode = new ListNode();
                index.next = newNode;
            } else if (l2.next == null && isPre) {
                newNode = new ListNode(1);
                index.next = newNode;
                isPre = false;
            }
            l2 = l2.next;
            index = newNode;
        }
        return root;
    }

    /**
     * 吴新悦写的 一个循环
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode l3 = new ListNode();
        ListNode index = l3;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            int l1v = l1 != null ? l1.val : 0;
            int l2v = l2 != null ? l2.val : 0;
            int sum = l1v + l2v + carry;
            carry = sum / 10;
            ListNode listNode = new ListNode(sum % 10);
            index.next = listNode;
            index = listNode;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        return l3.next;
    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}